
package com.android.formula.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;

import com.android.formula.R;
import com.android.formula.adapter.TBaseAdapter;
import com.android.formula.adapter.TypeSubSubjectListAdapter;
import com.android.formula.model.SubSubject;
import com.android.formula.model.TypeSubSubject;
import com.android.formula.task.GetTypeSubSubjectDBTask;
import com.android.formula.task.IDataEventHandler;
import com.android.formula.utils.CommonUtils;
import com.android.formula.utils.Constants;

public class TypeSubSubjectActivity extends BaseGridViewActivity<TypeSubSubject> {

    @Override
    public void initView() {
        super.initView();
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void clickOnPos(TypeSubSubject clickedItem, int pos) {
        Intent i = new Intent(this, FormulaActivity.class);
        Bundle bun = new Bundle();
        bun.putSerializable(Constants.DATA_KEY, clickedItem);
        i.putExtras(bun);
        startActivity(i);
    }

    @Override
    protected TBaseAdapter<TypeSubSubject> getAdapter() {
        return new TypeSubSubjectListAdapter(this, mListItemDatas);
    }

    @Override
    protected void getData() {
        super.getData();
        Bundle bun = getIntent().getExtras();
        SubSubject param = (SubSubject) bun.getSerializable(Constants.DATA_KEY);
        this.setTitle(param.getSubSubjectName());
        getDialogsManager().showProgress(R.string.loading_msg);

        GetTypeSubSubjectDBTask getTypeSubSubjectDBTask = new GetTypeSubSubjectDBTask(this, param,
                new IDataEventHandler<ArrayList<TypeSubSubject>>() {

                    @Override
                    public void onNotifyNoData() {
                        getDialogsManager().dismissProgress();
                        showNoData();
                    }

                    @Override
                    public void onNotifyErrorMessage(String title, String message) {
                        getDialogsManager().dismissProgress();
                        showNoData();
                        getDialogsManager().showErrorDialog(R.string.error_title,
                                R.string.error_msg, 0, null);
                    }

                    @Override
                    public void onNotifyData(ArrayList<TypeSubSubject> data) {
                        getDialogsManager().dismissProgress();
                        mListItemDatas.addAll(data);
                        if (mListItemDatas != null && mListItemDatas.size() > 0) {
                            showData();
                        } else {
                            showNoData();
                        }
                    }
                });
        CommonUtils.executeAsyTask(getTypeSubSubjectDBTask, param);
    }

}
